﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public sealed class SceneMa: Singleton<SceneMa>
{
    private SceneMa() { }



    public IEnumerator LoadScene(string scene)
    {
        Scene se = SceneManager.GetActiveScene();
        EventMa.I.notify(EGameEvent.ET_StartLoadScene, scene);
        if (se.name.Equals(scene))
        {
            EventMa.I.notify(EGameEvent.ET_EndLoadEndScene, scene);
        }
        else
        {
            bool isAsys = true;
            if (isAsys)
            {
                AsyncOperation async = SceneManager.LoadSceneAsync(scene);
                async.allowSceneActivation = true;
                yield return async;
                EventMa.I.notify(EGameEvent.ET_EndLoadEndScene, scene);
            }
            else
            {
                AsyncOperation async = SceneManager.LoadSceneAsync(scene);
                async.allowSceneActivation = false;
                int toProgress = 0;
                int showProgress = 0;

                //测试了一下，进度最大就是0.9  
                while (async.progress < 0.9f)
                {
                    toProgress = (int)(async.progress * 100);
                    while (showProgress < toProgress)
                    {
                        showProgress++;
                        yield return new WaitForEndOfFrame(); //等待一帧  
                    }
                }

                //计算0.9---1   其实0.9就是加载好了，我估计真正进入到场景是1    
                toProgress = 100;

                while (showProgress < toProgress)
                {
                    showProgress++;
                    yield return new WaitForEndOfFrame(); //等待一帧  
                }

                yield return new WaitForSeconds(2f);

                async.allowSceneActivation = true;  //如果加载完成，可以进入场景  
                yield return new WaitForSeconds(1f);
                yield return async;
                EventMa.I.notify(EGameEvent.ET_EndLoadEndScene, scene);
            }

       
        }
    }

    public int GetCurLevel()
    {
        Scene scene = SceneManager.GetActiveScene();
        return GetLevel(scene.name);
    }

    public int GetLevel(string sceneName)
    {
        switch (sceneName)
        {
            case DefineChar.scenes_01:
                return 0;
            case DefineChar.scenes_02:
                return 1;
            case DefineChar.scenes_03:
                return 2;
            case DefineChar.scenes_04:
                return 3;
            case DefineChar.scenes_05:
                return 4;
            case DefineChar.scenes_system:
                return 5;
        }

        Debug.LogError("传入场景等级错误");
        return 0;
    }

    public string GetSceneName(int lv)
    {
        switch (lv)
        {
            case 0:
                return DefineChar.scenes_01;
            case 1:
                return DefineChar.scenes_02;
            case 2:
                return DefineChar.scenes_03;
            case 3:
                return DefineChar.scenes_04;
            case 4:
                return DefineChar.scenes_05;
            case 5:
                return DefineChar.scenes_system;
        }

        Debug.LogError("传入场景错误");
        return string.Empty;
    }


}
